package com.hb.umsp.contraller;

import com.github.pagehelper.PageInfo;
import com.hb.umsp.entity.*;
import com.hb.umsp.service.ExamService;
import com.hb.umsp.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@CrossOrigin("*")
@RestController
@RequestMapping("/exam")
public class ExamController {

    @Autowired
    ExamService examService;

    @RequestMapping("/selectExam")
    public JsonResult selectExam(HttpServletRequest request,
                                 @RequestBody(required = false) HbExam hbExam,
                                 @RequestParam(defaultValue = "1") int pageNum,
                                 @RequestParam(defaultValue = "5" ) int pageSize){
        String token = request.getHeader("token");

        PageInfo<HbExam> page = examService.selectExam(hbExam,token,pageNum,pageSize);

        return new JsonResult("20000",page);
    }

    @RequestMapping("/selectExamDetail")
    public JsonResult selectExamDetail(@RequestParam String examCode){

        List<HbExamDetail> list = examService.selectExamDetail(examCode);

        return new JsonResult("20000",list);
    }

    @RequestMapping("/publish")
    public JsonResult publicExam(@RequestParam String examCode){

        HbExam hbExam = new HbExam();
        hbExam.setExamCode(examCode);
        hbExam.setIspublic("02"); //发布

        examService.publishExam(hbExam);
        return new JsonResult("20000","发布成功");
    }

    @RequestMapping("/random")
    public JsonResult selectRandom(@RequestBody(required = false) List<HbExamCreateType> hbExamCreateTypeList, HttpServletRequest request){

        String token = request.getHeader("token");
        List<HbQuestionBank> list = examService.selectRandomList(hbExamCreateTypeList,token);

        return new JsonResult("20000",list);
    }

    @RequestMapping("/create")
    public JsonResult createExam(@RequestBody(required = false) List<HbExamCreateType> hbExamCreateTypeList,
                                 HttpServletRequest request,
                                 @RequestParam String examName){

        String token = request.getHeader("token");
        if (!examService.insertExam(hbExamCreateTypeList,token,examName)) {
            return new JsonResult("00000","数据已经生成");
        }

        return new JsonResult("20000","生成成功");

    }

}
